<template>
  <div class="login-container">
    <div class="container">
      <div class="login">
        <h1 style="text-align: center;">登录</h1>
        <el-form :label-position="labelPosition" label-width="100px" :model="formLabelAlign">
          <el-form-item label="用户名">
            <el-input v-model="formLabelAlign.username"></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="formLabelAlign.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button class="login-btn" type="primary" @click="handleLogin">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import {submitLogin} from '@/api/login/index.js'
export default {
  data() {
    return {
      labelPosition: 'right',
      formLabelAlign: {
        username: '',
        password: ''
      }
    };
  },
  methods: {
    handleLogin() {
      if(this.formLabelAlign.username == 'admin'&&this.formLabelAlign.password == '123456') {
        this.$router.push('/admin')
        localStorage.setItem('adminor',JSON.stringify(this.formLabelAlign))
      }else{
        submitLogin(this.formLabelAlign).then( res => {
        // console.log(res.data)
        if(res.data.code == 20000) {
          this.$message.success('登陆成功');
          this.$store.dispatch('saveLogin',res.data.data)
          // console.log(localStorage.getItem('user'))
          
          setTimeout(() =>{
            this.$router.push('/home')
          },500)
        }else{
          this.$message.error('用户名或密码错误');
          localStorage.clear()
          // console.log(localStorage.getItem('user')); 
        }
      } )
      }
      
    }
  }
}
</script>

<style scoped>
.container {
  width: 100%;
  height: 600px;
}
.login {
  width: 400px;
  margin: 200px auto;
}
.login-btn {
  margin-left: 115px; 
}
</style>